import 'dart:developer';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:sports_new/entity/record/recent_channel_entity.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:umeng_apm_sdk/umeng_apm_sdk.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:scrollview_observer/scrollview_observer.dart';
import '../../entity/record/channel_entity.dart';
import '../../server/api/record_api.dart';
import '../../utils/user.dart';

class HotChannelLogic extends GetxController{
  int _channelType = 0;
  final scrollController = ApmScrollController();
  final leftController = ApmScrollController();
  final EasyRefreshController refreshController = EasyRefreshController();
  ScrollController? _bodyController;
  late SliverObserverController contentController =
    SliverObserverController(controller: scrollController);
  BuildContext? _listContext;
  List<RecentChannelEntity>? recentChannels;
  List<ChannelAreaEntity>? areas;
  ChannelAreaEntity? _area;
  String _selectCountry = "";
  List<String> topTabs = ["近期赛事","最近访问"];
  int _topIndex = 0;

  String get selectCountry => _selectCountry;
  ChannelAreaEntity? get area => _area;
  int get channelType => _channelType;
  BuildContext? get listContext => _listContext;
  ScrollController? get bodyController => _bodyController;
  int get topIndex => _topIndex;

  set topIndex(int value) {
    _topIndex = value;
    update();
  }
  set bodyController(ScrollController? value) {
    _bodyController = value;
    update();
  }
  set listContext(BuildContext? value) {
    _listContext = value;
    update();
  }
  set channelType(int value) {
    _channelType = value;
    update();
  }
  set area(ChannelAreaEntity? value) {
    _area = value;
    update();
  }
  set selectCountry(String value) {
    _selectCountry = value;
    update();
  }

  @override
  onReady(){
    getRecent();
    requestChannelsAll();
  }

  Future getRecent() async{
    var list = await RecordApi.recentChannel(channelType+1);
    if(list.hasValue) {
      recentChannels = list;
      if (list.length > 9) {
        recentChannels = recentChannels?.sublist(0, 9);
      }
    }
    update();
  }

  Future requestChannelsAll() async {
    final areaList = await RecordApi.channelsAll(channelType);
    if (areaList == null) {
      return;
    }
    areas = areaList;
    area = areaList
        .firstWhereOrNull((element) => element.area == (area?.area ?? "欧洲"));
    if (area == null && areaList.isNotEmpty) {
      area = areaList.first;
    }
    if (area != null &&
        (area?.list?.isNotEmpty ?? false) &&
        selectCountry.isEmpty) {
      selectCountry = area?.list?.first.country ?? "";
    }
    update();
  }

  void observerList(ListViewObserveModel model){
    final countries = area?.list;
    final index = model.firstChild?.index;
    if (countries != null &&
        index != null &&
        countries.length > index) {
      selectCountry = countries[index].country ?? "";
      final offset = index * 44.0;
      if (leftController.offset > offset) {
        leftController.animateTo(offset,
            duration: const Duration(milliseconds: 200),
            curve: Curves.linear);
      } else if (leftController.offset + 300 < offset) {
        leftController.animateTo(offset - 300,
          duration: const Duration(milliseconds: 200),
          curve: Curves.linear
        );
      }
      update();
    }
  }

  void doRefresh(){
    if(refreshController.headerState?.mode.isIdle == false) return;
    refreshController.callRefresh(scrollController: scrollController);
  }
}